package com.grad.webstudy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grad.webstudy.constants.CourseTypeConstants;
import com.grad.webstudy.constants.RedisConstants;
import com.grad.webstudy.context.UserContext;
import com.grad.webstudy.dao.CoursetypeDao;
import com.grad.webstudy.enums.ExceptionEnums;
import com.grad.webstudy.exceptions.CustomException;
import com.grad.webstudy.model.CourseType;
import com.grad.webstudy.service.CourseTypeService;
import com.grad.webstudy.utils.RedisUtil;
import com.grad.webstudy.vo.CourseTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description:
 * @Author: zjf
 * @Date: 2020/2/1 13:43.
 */
@Service
@Transactional
public class CourseTypeServiceImpl implements CourseTypeService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CoursetypeDao coursetypeDao;

    @Override
    public boolean addCourseType(String typeName, int parentId, int type) {
        CourseType courseType = new CourseType();

        courseType.setParentId(parentId);
        courseType.setTypeName(typeName);
        courseType.setCreateTime(new Date());
        courseType.setCreator(UserContext.getLoginUser().getUsername());
        courseType.setType(type);
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSETYPE_KEY_PREFIX);
        return coursetypeDao.insert(courseType) > 0;
    }

    @Override
    public boolean deleteCourseType(String ids) {

        List<String> typeIds = Arrays.asList(ids.split(","));

        if (typeIds.size() <= 0) {
            throw new CustomException(ExceptionEnums.CUSTOM_ERROR.getCode(),
                    "参数ids" + ids + "有误");
        }
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSETYPE_KEY_PREFIX);
        return coursetypeDao.deleteCourseTypeByIds(typeIds) == typeIds.size();
    }

    @Override
    public boolean updateCourseType(CourseType courseType) {
        redisUtil.delByPrefix(RedisConstants.REDIS_COURSETYPE_KEY_PREFIX);
        return coursetypeDao.updateCourseType(courseType) > 0;
    }

    @Override
    public PageInfo queryAllCourseType(Integer currPage,
                                       Integer pageSize,
                                       Integer type,
                                       String keyword) {

        String key = RedisConstants.REDIS_COURSETYPE_KEY_PREFIX + "queryAll" + currPage + "-" + pageSize + "-" + type+"-"+keyword;
        PageInfo<CourseTypeVo> pageInfo;
        if (redisUtil.hasKey(key)) {
            pageInfo = (PageInfo<CourseTypeVo>) redisUtil.get(key);
        } else {
            Page page = PageHelper.startPage(currPage, pageSize);

            pageInfo = new PageInfo<>(convert(coursetypeDao.queryTypeByType(type,keyword)));

            pageInfo.setTotal(page.getTotal());
            pageInfo.setPageNum(currPage);
            pageInfo.setPageSize(pageSize);
            redisUtil.set(key, pageInfo);

        }

        return pageInfo;
    }

    @Override
    public List<CourseType> queryAll() {
        return coursetypeDao.selectAllCourseType();
    }

    @Override
    public String queryCourseTypeName(String courseTypeIds) {

        List<String> ids = Arrays.asList(courseTypeIds.split(","));

        return String.join(",", coursetypeDao.selectCourseTypeNameByIds(ids));
    }

    @Override
    public List<Integer> queryChildType(Integer parentId) {

        List<Integer> result = new ArrayList<>();
        List<CourseType> courseTypes = coursetypeDao.queryChildType(parentId);
        for (CourseType courseType : courseTypes) {
            result.add(courseType.getId());
        }
        result.add(parentId);

        return result;
    }

    @Override
    public List<CourseType> queryChild(Integer parentId) {
        return coursetypeDao.queryChildType(parentId);
    }

    @Override
    public List<CourseType> queryDire() {
        return coursetypeDao.queryDireType();
    }

    @Override
    public List<CourseType> queryAllChild() {
        return coursetypeDao.queryAllChild();
    }

    @Override
    public Integer queryParentId(Integer childId) {
        return coursetypeDao.queryParentId(childId);
    }

    @Override
    public Integer queryTypeByKeyword(String keyword) {
        return coursetypeDao.queryTypeByKeyword(keyword);
    }


    /**
     * model 转换为 vo
     *
     * @param courseTypes
     * @return
     */
    private List<CourseTypeVo> convert(List<CourseType> courseTypes) {

        List<CourseTypeVo> result = new ArrayList<>(courseTypes.size());

        HashMap<Integer, String> name = new HashMap<>();

        List<CourseType> parentList = queryDire();

        for (CourseType courseType : parentList) {
            name.put(courseType.getId(), courseType.getTypeName());
        }

        for (CourseType courseType : courseTypes) {

            if (courseType.getParentId() == CourseTypeConstants.PARENT_ID) {
                result.add(new CourseTypeVo.Builder()
                        .setType(courseType.getType())
                        .setCreateTime(courseType.getCreateTime())
                        .setParentName("无")
                        .setTypeName(courseType.getTypeName())
                        .setCreator(courseType.getCreator())
                        .setId(courseType.getId())
                        .setParentId(courseType.getParentId())
                        .setTypeVo("方向")
                        .build());
            } else {
                result.add(new CourseTypeVo.Builder()
                        .setType(courseType.getType())
                        .setCreateTime(courseType.getCreateTime())
                        .setParentName(name.get(courseType.getParentId()))
                        .setTypeName(courseType.getTypeName())
                        .setCreator(courseType.getCreator())
                        .setId(courseType.getId())
                        .setParentId(courseType.getParentId())
                        .setTypeVo("类型")
                        .build());
            }
        }
        return result;

    }


}
